//
//  NADAMobileIOHelper.h
//  TMI
//
//  Created by Brian on 7/14/09.
//  Copyright 2009 Tellart. All rights reserved.
//

#include <MediaPlayer/MediaPlayer.h>
#include <AudioToolbox/AudioToolbox.h>
#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>
#import "ASIHTTPRequest.h"
#import "ASIFormDataRequest.h"
#import "ASINetworkQueue.h"
#import "P2PMessenger.h"

@class ASINetworkQueue;

/*
// ifdef __cplusplus 
// import "AM_IO.h"
// endif
*/
#import "NewAMIO.h"

#define kAccelerometerPeriod            0.040f
#define kTwitpicUploadAndPostURL        @"http://twitpic.com/api/uploadAndPost"

#define kSimpleSocketTCPPort            (unsigned short)65000

#define IOHelperSimpleSocketStatus      int
#define kIOHelperSimpleSocketStatus_OK  0


@class ASINetworkQueue;

@protocol NADAMobileIOHelperDelegate;

@interface NADAMobileIOHelper : NSObject
    <UIAccelerometerDelegate,
    UIWebViewDelegate,
    CLLocationManagerDelegate,
    P2PMessengerDelegate>
{
    id <NADAMobileIOHelperDelegate> myDelegate;

    //enabled/disabled state of various sensors
    BOOL sensorLocationEnabled;
    BOOL sensorAccelerometerEnabled;
    BOOL sensorAudioAmplitudeEnabled;
    BOOL sensorProximityEnabled;
    BOOL sensorOrientationEnabled;
    BOOL sensorBatteryEnabled;
    BOOL sensorCameraEnabled;
    BOOL sensorIPodRemoteEnabled;
    
    //location sensing ivars
	CLLocationManager *locationManager;
    
    //UIImagePickerController *cameraController;
    
    //audio amplitude ivars
    AMIOAmplitudeValue lastPeakAmp;
    
    //audio player ivars
    AVAudioPlayer *audioPlayer;
/*
// ifdef __cplusplus 
    AM_IO *amIO;
    NewAMIO *amIO;
    NSTimer *amIOTimer;
// endif
*/

    //video player ivars
    MPMoviePlayerController *videoPlayer;
    
    //iPod remote ivars
    MPMusicPlayerController *musicPlayer;

    //ivars for IO via the audio input
    NewAMIO *amIO;

    NSMutableDictionary *soundIds;

    //twitter vars
	ASINetworkQueue *networkQueue;
    
    //simple socket vars
    BOOL simpleSocketKeepAccepting;
    
    //GK peer messaging vars
    P2PMessenger *peerMessenger;
}

@property (nonatomic, assign) id <NADAMobileIOHelperDelegate> myDelegate;
//@property (nonatomic, retain) UIImagePickerController *cameraController;
@property (nonatomic, retain) AVAudioPlayer *audioPlayer;
@property (nonatomic, retain) MPMoviePlayerController *videoPlayer;
@property (nonatomic, retain) MPMusicPlayerController *musicPlayer;

- (void) enableWithDelegate:(id <NADAMobileIOHelperDelegate>)delegate
    useLocation:(BOOL)useLocation
    useAccelerometer:(BOOL)useAccelerometer
    useAudio:(BOOL)useAudio:(BOOL)generateCarrierTone:(NSString*)carrierToneFilePath
    useProximity:(BOOL)useProximity
    useOrientation:(BOOL)useOrientation
    useBattery:(BOOL)useBattery
    useIPodRemote:(BOOL)useIPodRemote
    //useCamera:(BOOL)useCamera
;
- (void)setDelegate:(id <NADAMobileIOHelperDelegate>)delegate;
- (id <NADAMobileIOHelperDelegate>)getDelegate;
- (void)disable;
- (void)enableSensorLocation;
- (void)disableSensorLocation;
- (void)disableSensorAccelerometer;
- (void)enableSensorAccelerometer;
- (void)enableSensorAudioAmplitude:(BOOL)generateCarrierTone withSampleFile:(NSString*)pathToCarrierToneFile;
- (void)disableSensorAudioAmplitude;
- (void)enableSensorProximity;
- (void)disableSensorProximity;
- (void)enableSensorOrientation;
- (void)disableSensorOrientation;
- (void)enableSensorBattery;
- (void)disableSensorBattery;
- (void)enableSensorIPodRemote;
- (void)disableSensorIPodRemote;
//- (void)enableSensorCamera;
//- (void)disableSensorCamera;

//- (void)beginCameraCaptureFromViewController:(UIViewController *)vc;

- (void) updateAmplitudeMonitor;
- (void) playSound:(NSURL*)soundURL;
- (void) stopAllSounds;
- (void) vibrate;

- (void) playVideo:(NSURL*)videoURL;
- (void) stopAllVideos;
- (void) videoPlayerFinishedCallback:(NSNotification*)aNotification;

- (void)sendTwitpicWithNetworkQueueWithUsername:(NSString*)username /*:(ASINetworkQueue*)queue*/
        andPassword:(NSString*)password
        andMessage:(NSString*)message
        andImage:(UIImage*)image;

- (IOHelperSimpleSocketStatus) simpleSocketAccept;
- (IOHelperSimpleSocketStatus) simpleSocketSendTo:(NSString *)addr message:(NSString *)msg async:(BOOL)a;


- (void) messengerAccept:(NSString *)peerID;
- (void) messengerSendTo:(NSString *)toID from:(NSString *)fromID message:(NSString *)msg;
- (void) messenger:(P2PMessenger *)messenger didReceiveData:(NSData *)data from:(NSString *)fromID;

@end


@protocol NADAMobileIOHelperDelegate
- (void)nada:(NADAMobileIOHelper *)nada didAccelerate:(UIAcceleration*)acceleration;
- (void)nada:(NADAMobileIOHelper *)nada didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation;
- (void)nada:(NADAMobileIOHelper *)nada didUpdateAudioAmplitude:(AMIOAmplitudeValue)amp average:(AMIOAmplitudeValue)avg low:(AMIOAmplitudeValue)l mid:(AMIOAmplitudeValue)m high:(AMIOAmplitudeValue)h;
- (void)nada:(NADAMobileIOHelper *)nada didChangeProximity:(BOOL)isNearObject;
- (void)nada:(NADAMobileIOHelper *)nada didChangeOrientation:(UIDeviceOrientation)orientation;
- (void)nada:(NADAMobileIOHelper *)nada didChangeBattery:(float)batteryLevel;
- (void)nada:(NADAMobileIOHelper *)nada didReceiveSimpleSocketMessage:(NSString *)msg from:(NSString *)addr;
- (void)nada:(NADAMobileIOHelper *)nada didReceiveFromMessenger:(NSString *)msg from:(NSString *)fromID;
- (void)nada:(NADAMobileIOHelper *)nada iPodDidChangeStatePlaying:(BOOL)playing paused:(BOOL)paused;
- (void)nada:(NADAMobileIOHelper *)nada didChangeIPodVolume:(float)volume;

#if __IPHONE_3_0
- (void)nada:(NADAMobileIOHelper *)nada didUpdateHeading:(CLHeading *)newHeading;
#endif

@end
